//
//  YPNotificationManagerViewController.m
//  ChengHuiTong
//
//  Created by KiVen on 15/12/1.
//  Copyright © 2015年 张文权. All rights reserved.
//

#import "YPNotificationManagerViewController.h"

@interface YPNotificationManagerViewController ()<UITableViewDataSource,UITableViewDelegate>{
    NSArray * _titleArray;
    
    
    UITableView *_notificationManagerTableView;
}

@end

@implementation YPNotificationManagerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _titleArray = @[@"还款短信提醒",@"站内信通知"];

    self.navigationItem.title = @"通知管理";
    _notificationManagerTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStyleGrouped];
    _notificationManagerTableView.dataSource = self;
    _notificationManagerTableView.delegate = self;
    _notificationManagerTableView.contentInset = UIEdgeInsetsMake(-20, 0, 0, 0);
    _notificationManagerTableView.sectionHeaderHeight =15.0f;
    _notificationManagerTableView.sectionFooterHeight = 0.0f;
    _notificationManagerTableView.backgroundColor = kColor_AppBaseBackground;

    _notificationManagerTableView.tableFooterView= ({
    
        UIView * tableFootView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
        
        tableFootView.backgroundColor = [UIColor whiteColor];
        
        UILabel * titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, SCREEN_WIDTH-20, 40)];
        titleLabel.font = [UIFont systemFontOfSize:13];
        titleLabel.textColor = UIColorFromRGB(0xd3d2d2);
        titleLabel.text = @"关闭通知后,将不会在通知栏推送站内信";
    
        [tableFootView addSubview:titleLabel];
    
        tableFootView;
    });
    
    [self.view addSubview:_notificationManagerTableView];

}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    [MobClick beginLogPageView:@"通知管理"];
    
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:YES];
    [MobClick endLogPageView:@"通知管理"];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 2;
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    if (!cell) {
        
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.frame = CGRectMake(10, 0, 150, 40);
       
        
        
        UISwitch * remindSwitch = [[UISwitch alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-15-60, 5, 60, 30)];
        remindSwitch.tag = 100+indexPath.section;
        
        [remindSwitch addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];

        [cell.contentView addSubview:remindSwitch];
        
        
        cell.textLabel.font=[UIFont systemFontOfSize:15];
        
        
        
    }
    
    cell.textLabel.text=_titleArray[indexPath.section];
    
//    还款提醒
    UISwitch *repaymentRemindSwitch = [cell.contentView viewWithTag:100];
    [repaymentRemindSwitch setOn:[[LGToolMethods getUserDefaultsObjForKey:@"remindRepayment"] boolValue] animated:YES];
//    站内信提醒
    UISwitch *stationMessageSwitch = [cell.contentView viewWithTag:101];
    [stationMessageSwitch setOn:[[LGToolMethods getUserDefaultsObjForKey:@"station"] boolValue] animated:YES];


    return cell;
    
}
-(void)switchAction:(UISwitch *)sender{
    
    YPNotificationManagerBin * bin = [[YPNotificationManagerBin alloc]init];
    bin.token = [YPShareAccountInfo shareAccountInfo].token;
     bin.status = sender.on == YES?@"0":@"1";
    if (sender.tag == 100) {

            bin.type = @"0";
        
            [YPHttpRequestMethod sendAsyRequestURL:CMD_NOTIFICATION_MANAGER withData:[bin dictionaryValue] withCompletionBlock:^(NSDictionary *responseDictionary) {
                
                NSLog(@"---%@",responseDictionary);
                BOOL success = [responseDictionary[@"status"] integerValue] == 0?YES:NO;
                
                [sender setOn:success?sender.on:!sender.on animated:YES];
          
                [LGToolMethods saveUserDefaultsObj:@(sender.on) forKey:@"remindRepayment"];
                
                
            } withFailedBlock:^(NSError *error) {
                
                [sender setOn:!sender.on animated:YES];
                
                [LGToolMethods saveUserDefaultsObj:@(sender.on) forKey:@"remindRepayment"];
                
            } withIndicator:YES atView:self.view];

    }else if(sender.tag == 101){

        bin.type = @"1";
        
        [YPHttpRequestMethod sendAsyRequestURL:CMD_NOTIFICATION_MANAGER withData:[bin dictionaryValue] withCompletionBlock:^(NSDictionary *responseDictionary) {
            
            BOOL success = [responseDictionary[@"status"] integerValue] == 0?YES:NO;
            
            [sender setOn:success?sender.on:!sender.on animated:YES];
            
            [LGToolMethods saveUserDefaultsObj:@(sender.on) forKey:@"station"];
            
        } withFailedBlock:^(NSError *error) {
            
            [sender setOn:!sender.on animated:YES];
            
            [LGToolMethods saveUserDefaultsObj:@(sender.on) forKey:@"station"];
            
        } withIndicator:YES atView:self.view];

        
    }
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
